﻿using System;
using System.Collections.Generic;
using System.Text;
using Xeno.Core;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using Xeno.Module;
using XText = Xeno.Core.XenoText.XenoTextCache; //just a simple hack to speed up code dev. Use it wisely.
using System.Drawing;
using Xeno.GUI;
using OpenTK.Math;

namespace XenoCraft
{
    public class XenoIDE : XenoApp
    {


        public XenoNode XenoDesktop;
        public XenoNode ToolGroup;
        public XenoNode QuitBut;
        public XenoNode SceneView;

        public XText XenoLogo;

        public XenoNode View;
        public XenoNode Cube;
        public XenoNode Room;
        public XenoNode Light;
        public XenoNode Light2;
        public float CubeYaw = 0;
        public float LightAng = 0.0f;
        public float LightAng2 = 130.0f;
        public XenoNode TestModel;

        public float CamPitch = 0;
        public float CamYaw = 0;
        public XenoNode Floor;
        public XenoGraph EditScene;
        public List<XenoNode> Spiders = new List<XenoNode>();
        public XenoIDE()
            : base(1280, 900, "Xeno3D Community Technology Preview 1 (c)Double Think Games 2009/10")
        {

        }
        public override void DoInit()
        {

            EditScene = new XenoGraph("ActiveWorld");

            XenoDesktop = XenoFactory.Xui.Desktop("XenoOS", XenoCore.CurrentScene.RootNode);

            Cube = XenoFactory.Node.Mesh("Cube1", null);
            Room = XenoFactory.Node.Mesh("Room", EditScene.RootNode);

            XenoFactory.Mesh.Pyramid(Cube, 5);
            XenoFactory.Mesh.Pyramid(Room, 0.3f);

            ToolGroup = XenoFactory.Xui.Group("ToolGroup", XenoCore.CurrentScene.RootNode, "Tools", new Vector2(5, 5), new Vector2(800, 750));
            //QuitBut = XenoFactory.Xui.Button("QuitButton", ToolGroup, "QuitOS", new Vector2(5, 5), new Vector2(80, 20));
            SceneView = XenoFactory.Xui.GraphView("SceneView", ToolGroup, new Vector2(10,10), new Vector2(792, 742),EditScene);



            //XenoLogo = XenoText.CreateText("Xeno3D (c)Double Think Games 2009");




            Floor = XenoFactory.Node.Mesh("Floor", EditScene.RootNode);
            XenoMesh msh = Floor.Mesh;

            float rs = 150.0f;

            XenoVertex v0 = new XenoVertex(new Vector3(-rs, 0, -rs), new Vector3(0, 1, 0), new Vector3(0, 0, 0));
            XenoVertex v1 = new XenoVertex(new Vector3(rs, 0, -rs), new Vector3(0, 1, 0), new Vector3(1, 0, 0));
            XenoVertex v2 = new XenoVertex(new Vector3(rs, 0, rs), new Vector3(0, 1, 0), new Vector3(1, 1, 0));
            XenoVertex v3 = new XenoVertex(new Vector3(-rs, 0,rs), new Vector3(0, 1, 0), new Vector3(0, 1, 0));

            msh.AddVertex = v0;
            msh.AddVertex = v1;
            msh.AddVertex = v2;
            msh.AddVertex = v3;

     

            msh.AddTriangle = new XenoTriangle(0, 1, 2);
            msh.AddTriangle = new XenoTriangle(2, 3, 0);

            Floor.MeshRenderer.Material = new XenoMaterial("FloorMat");

            XenoFactory.Physics.Cube(Floor,0);
            int sc = 40;
            Random rnd = new Random(3534);
        //    for (int i = 0; i < sc; i++)
          //  {

                
                TestModel = XenoImport.ImportObj("Content/Model/spider.obj", "TestModel", EditScene.RootNode, new Vector3(0.1f, 0.1f, 0.1f));
                Spiders.Add(TestModel);
                TestModel.LocalPosition = new Vector3(0, 20, 0); // new Vector3(rnd.Next(-50, 50), rnd.Next(5, 25), rnd.Next(-50, 50));
               // TestModel.Rotate(rnd.Next(0, 360), rnd.Next(0, 360), 0);
                TestModel.Rotate(40, 0, 0);
            XenoFactory.Physics.Cube(TestModel, 1f);
            //}

            View = XenoCore.CurrentView;


          
            //XenoDebug.LogError("TModel Verts:" + TestModel.Mesh.Vertices.Count + " Tris:" + TestModel.Mesh.Triangles.Count, "");
            //TestModel.MeshRenderer.Material.SpecularColor = new Vector4(1, 1, 0, 1);
            //TestModel.MeshRenderer.Material.SpecularPower = 0.4f;

            //Room.MeshRenderer.Material.SpecularColor = new Vector4(0, 1, 1, 1);
            //Room.MeshRenderer.Material.SpecularPower = 0.25f;
            View.LocalPosition = new Vector3(0, 2, 25);

            Light = XenoFactory.Light.Directional("DirLight1", EditScene.RootNode);
            Light2 = XenoFactory.Light.Directional("DirLight2", EditScene.RootNode);
           

            Light.Light.Direction = new Vector3(0, 0, -1);
            Light.Light.DiffuseColor = Color.FloralWhite;
            Light2.Light.DiffuseColor = Color.Violet;
           

            base.DoInit();
        }
        public override void DoUpdate()
        {
            CubeYaw += 1;
            //Cube.Rotate(0, CubeYaw, 0);
        
            LightAng += 0.01f;
            LightAng2 -= 0.02f;
            Light.Light.Direction = new Vector3((float)Math.Cos(LightAng), (float)Math.Sin(LightAng),(float) Math.Sin(LightAng));
            Light2.Light.Direction = new Vector3((float)Math.Cos(LightAng2), 0, (float)Math.Sin(LightAng2));
            CamPitch -= XenoInput.MouseYI();
            CamYaw -= XenoInput.MouseXI();

            //CamPitch = XenoInput.MouseY();


      //      View.Rotate(CamPitch, CamYaw, 0);


            base.DoUpdate();
        }
        public override void DoPreRender()
        {
            
            base.DoPreRender();
        }
        public override void DoPostRender()
        {

            //XenoText.DrawText(XenoLogo, 5, 5,Color.Black);

            base.DoPostRender();
        }
    }
   
    public class Program
    {

        public static XenoIDE Ide = null;

        static void Main(string[] args)
        {

            Ide = new XenoIDE();

            Ide.Run(60);

        }
    }
}
